package com.atdel.demo16;


import org.junit.Test;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @BelongsProject: java_study
 * @BelongsPackage: com.atdel.demo16
 * @Author: Del
 * @CreateTime: 2025-03-17  15:26
 * @Description: 练习分别使用四种方法获取类的 Class 对象，进行相关的操作
 * @Version: 1.0
 */
public class ReflectDemo {
    
    @Test
    public void test01() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        // 1. 通过类的对象获取
        Student student = new Student("张三", 18);
        Class<? extends Student> aClass = student.getClass();
        System.out.println(aClass);

        // 2. 通过类的全限定名获取
        try {
            Class<?> aClass1 = Class.forName("com.atdel.demo16.Student");
            System.out.println(aClass1);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        // 3. 通过类的类加载器获取
        ClassLoader classLoader = ClassLoader.getSystemClassLoader();
        Class<?> aClass2 = classLoader.loadClass("com.atdel.demo16.Student");
        System.out.println(aClass2);


        // 4. 通过类的静态属性获取
        Class<Student> studentClass = Student.class;
        System.out.println(studentClass);
    }
}
